package com.gibs.peixunapi.controller;

import com.gibs.peixunapi.VO.CreateRecordVO;
import com.gibs.peixunapi.VO.EditTestPaperVO;
import com.gibs.peixunapi.VO.StudyRecordVO;
import com.gibs.peixunapi.VO.SubjectHubVO;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.excepiton.BaseException;
import com.gibs.peixunapi.model.TestPaper;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.service.*;
import com.gibs.peixunapi.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 功能： 题库中心
 * 项目：装配式培训平台
 * 日期：2020-08-20
 * 作者：梁嘉伟
 */

@Slf4j
@Api(tags = "题目中心API")
@RestController
@RequestMapping("/SubjectHubs")
public class SubjectHubs extends Application {

    @Autowired
    private SubjectHubService subjectHubService;
    @Autowired
    private CourseSubjectHubService courseSubjectHubService;
    @Autowired
    private ExamInfoService examInfoService;
    @Autowired
    private ExamClassInfoService examClassInfoService;
    @Autowired
    private TestPaperService testPaperService;
    @Autowired
    private TestPaperSubjectService testPaperSubjectService;
    @Autowired
    private ExercisesService exercisesService;
    @Autowired
    private ExerciseRecordService exerciseRecordService;
    @Autowired
    private StudyProgressService studyProgressService;


    @Transactional
    @ApiOperation(value = "创建单条题目")
    @PostMapping("/createSingleSubject")
    public Result createSingleSubject(@RequestBody SubjectHubVO object) {
        return subjectHubService.create(object);
    }

    @Transactional
    @ApiOperation(value = "编辑单条题目")
    @PostMapping("/editSubject")
    public Result editSubject(@RequestBody SubjectHubVO object) {
        return subjectHubService.save(object);
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除单条题目")
    @ApiImplicitParam(name = "id", value = "题目id", dataType = "int")
    public Result delete(@RequestBody Integer id) {
        return subjectHubService.delete(id);

    }

    @GetMapping("/linkCourseSubjectHub")
    @ApiOperation(value = "关联课程习题库")
    @ApiImplicitParam(name = "courseId", value = "课程Id", dataType = "int")
    public Result linkCourseSubjectHub(Integer courseId) {
        return courseSubjectHubService.linkSubjectHubByCourseId(courseId);
    }//TODO waiting to code

    @GetMapping("/getTestPaper")
    @ApiOperation(value = "获取试卷")
    @ApiImplicitParam(name = "examInfoId", value = "考试Id", paramType = "query", dataType = "int")
    public Result getTestPaper(@RequestParam Integer examInfoId) {
        TestPaper testPaper = examInfoService.getTestPaper(examInfoId);
        Map<String, List<SubjectHubVO>> subjectHubVOMap = testPaperSubjectService.getMapByTestPaper(testPaper.getId());
        return testPaperService.getVOList(testPaper, subjectHubVOMap);
    }//TODO waiting for test

    @PostMapping("/editTestPaperAndSubject")
    @ApiOperation(value = "编辑试卷 前段传递需要更改的题目列表")
    public Result editTestPaperAndSubject(@RequestBody EditTestPaperVO editTestPaperVO) {
        if (editTestPaperVO.getSubjectHubVOList().isEmpty()) {
            throw new BaseException(ResultEnum.PARAM_ERROR);
        }
        //获取原来卷子的题目list
        Map<String, List<SubjectHubVO>> subjectHubVOMap = testPaperSubjectService.getMapByTestPaper(editTestPaperVO.getTestPaperId());
        List<SubjectHubVO> subjectHubVOList = new ArrayList<>();
        subjectHubVOMap.forEach((key, value) -> subjectHubVOList.addAll(value));

        if (examClassInfoService.isTestPaperUsed(editTestPaperVO.getExamInfoId())) {
            // 如果此试卷已经使用,根据传来的id进行替换并新增试卷,新增试卷题目中间表
            TestPaper testPaper = testPaperSubjectService.switchTestPaperSubject(subjectHubVOList, editTestPaperVO);
            //更新examInfo
            return examInfoService.updateTestPaper(testPaper);
        } else {//如果此试卷并未使用那么就直接更改题目
            return testPaperSubjectService.updateTestPaperSubject(subjectHubVOList, editTestPaperVO);
        }
    }

    @GetMapping("/getSubjectInCourse")
    @ApiOperation(value = "根据课程id和目标类型Type获取习题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId", value = "课程Id", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "type", value = "类型", paramType = "query")
    })
    public Result getSubjectInCourse(@RequestParam Integer courseId, @RequestParam(required = false) String type) {

        return courseSubjectHubService.getSubjectList(courseId, type);
    }


    @GetMapping("/getChapterExercise")
    @ApiOperation(value = "学员获取此章节的练习题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chapterId", value = "章节Id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "type", value = "课程类型 文档:Document 视频:video", paramType = "query")
    })
    public Result getChapterExercise(@RequestParam Integer chapterId, @RequestParam String type) {
        List<Integer> idList = exercisesService.getSubjectHubIdList(chapterId, type);
        return subjectHubService.getVOListByIdList(idList);
    }

    @PostMapping("/getExerciseRecord")
    @ApiOperation(value = "学员获的练习题记录/错题集 //waiting to test")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int"),
            @ApiImplicitParam(name = "isRight", value = "是否选择错题集", dataType = "int"),
            @ApiImplicitParam(name = "courseId", value = "课程id", dataType = "int"),
            @ApiImplicitParam(name = "chapterId", value = "章节id", dataType = "int"),
            @ApiImplicitParam(name = "type", value = "类型"),
            @ApiImplicitParam(name = "page", value = "页数", dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "最大条数", dataType = "int")
    })
    public Result getExerciseRecord(@RequestBody Integer studentId, @RequestBody String isRight, @RequestBody(required = false) Integer courseId,
                                    @RequestBody(required = false) Integer chapterId, @RequestBody(required = false) String type,
                                    @RequestBody Integer page, @RequestBody Integer limit) {
        return exerciseRecordService.getListPage(studentId, courseId, chapterId, type, page, limit);
        //TODO waiting to test
    }

    @PostMapping("/createExerciseRecord")
    @ApiOperation(value = "新增学员的习题回答记录,并且更新学生学习进度")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId", value = "学员id", dataType = "int"),
    })
    public Result createExerciseRecord(@RequestBody CreateRecordVO createRecordVO) {
        for (StudyRecordVO e : createRecordVO.getRecordVOList()) {
            if (StringUtils.isBlank(e.getMyAnswer())) {
                throw new BaseException(ResultEnum.PARAM_ERROR);
            }
        }
        //新增学员的习题回答记录
        exerciseRecordService.create(createRecordVO.getStudentId(), createRecordVO.getRecordVOList());
        //更新学生学习进度
        studyProgressService.saveProgress(createRecordVO.getStudentId(), createRecordVO.getRecordVOList());
        return ResultUtil.success(ResultEnum.SUCCESS);
    }

    @PostMapping("/getVideoClassProgress")
    @ApiOperation(value = "获取该学员在此视频的观看时长")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId", value = "学员id", required =true, dataType = "int"),
            @ApiImplicitParam(name = "videoClassId", value = "视频课程id", required =true,  dataType = "int"),

    })
    public Result getVideoClassProgress(@RequestParam Integer studentId, @RequestParam Integer videoClassId) {

        return studyProgressService.getVideoClassProgress(studentId,videoClassId);
    }

}

